
from vanna.flask import VannaFlaskApp
from openai import OpenAI
from vanna.base import VannaBase
from vanna.chromadb import ChromaDB_VectorStore
from vanna.openai import OpenAI_Chat

from pymilvus import MilvusClient
from vanna.milvus import Milvus_VectorStore

from src.config.constant import ALI_API_KEY, ALI_API_URL

class MyVanna(Milvus_VectorStore,OpenAI_Chat):
    def __init__(self, config=None):
        Milvus_VectorStore.__init__(self, config=config)
        # 创建自定义的 OpenAI 客户端
        client = OpenAI(
            base_url=config.get('base_url'),
            api_key=config.get('api_key'),
        )
        OpenAI_Chat.__init__(self,  client=client, config=config)
        # OpenAI_Chat.__init__(self, config=config)
        # GoogleGeminiChat.__init__(self, config={'api_key': 'AIzaSyBDnxuUPNnsJj1CaxXMRpkkinf2WfrsZzM','model': 'gemini-2.0-flash'})

config = {
    'model': 'qwen-plus',  # 你的模型名称
    'temperature': 0.7,
    'base_url': ALI_API_URL,  # 你的 OpenAI API 地址
    'api_key': ALI_API_KEY,  # 你的 API 密钥
    "milvus_client": MilvusClient(uri="http://localhost:19530")
}
vn = MyVanna(config)

vn.connect_to_mysql(host='localhost', dbname='vanna',
                    user='root', password='Talent@123',port=3306)
# The information schema query may need some tweaking depending on your database. This is a good starting point.
df_information_schema = vn.run_sql("SELECT * FROM INFORMATION_SCHEMA.COLUMNS")

# This will break up the information schema into bite-sized chunks that can be referenced by the LLM
plan = vn.get_training_plan_generic(df_information_schema)
plan

# If you like the plan, then uncomment this and run it to train
# vn.train(plan=plan)
# The following are methods for adding training data. Make sure you modify the examples to match your database.

# DDL statements are powerful because they specify table names, colume names, types, and potentially relationships
vn.train(ddl="""
    CREATE TABLE IF NOT EXISTS mytable (
        id INT PRIMARY KEY,
        name VARCHAR(100),
        age INT
    )
""")

''''
insert into mytable values (1, 'John Doe', 30);
insert into mytable values (2, 'li hao', 32);
insert into mytable values (3, 'wang long ', 35);
'''

# Sometimes you may want to add documentation about your business terminology or definitions.
vn.train(documentation="Our business defines OTIF score as the percentage of orders that are delivered on time and in full")

# You can also add SQL queries to your training data. This is useful if you have some queries already laying around. You can just copy and paste those from your editor to begin generating new SQL.
vn.train(sql="SELECT * FROM mytable WHERE name = 'John Doe'")

# At any time you can inspect what training data the package is able to reference
training_data = vn.get_training_data()
# training_data
print('====训练数据 %s' % training_data)


# You can remove training data if there's obsolete/incorrect information.
# vn.remove_training_data(id='1-ddl')

print('====提问')
vn.ask(question="How many users are there?")

print('====启动web')
app = VannaFlaskApp(vn)
app.run()
